package com.stylefeng.guns.modular.bus.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.Config;
import com.stylefeng.guns.core.util.KaptchaUtil;
import com.stylefeng.guns.core.util.ResponseData;
import com.stylefeng.guns.core.util.ServiceStatus;
import com.stylefeng.guns.modular.bus.service.IBusPrdTraceHistoryService;
import com.stylefeng.guns.modular.bus.service.IBusPrdTraceService;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.IDeptService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.bus.service.IBusPrdTraceInfoService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BusPrdTraceInfo控制器
 *
 * @author fengshuonan
 * @Date 2018-04-21 22:07:35
 */
@Controller
@RequestMapping("/busPrdTraceInfo")
public class BusPrdTraceInfoController extends BaseController {

    private String PREFIX = "/bus/busPrdTraceInfo/";

    @Autowired
    private IBusPrdTraceInfoService busPrdTraceInfoService;
    @Autowired
    private IDeptService iDeptService;
    @Autowired
    private IBusPrdTraceService busPrdTraceService;
    @Autowired
    private IBusPrdTraceHistoryService busPrdTraceHistoryService;
    @Autowired
    private IDeptService deptService;

    /**
     * 跳转到BusPrdTraceInfo首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "busPrdTraceInfo.html";
    }

    /**
     * 跳转到添加BusPrdTraceInfo
     */
    @RequestMapping("/busPrdTraceInfo_add")
    public String busPrdTraceInfoAdd() {
        return PREFIX + "busPrdTraceInfo_add.html";
    }

    /**
     * 跳转到修改BusPrdTraceInfo
     */
    @RequestMapping("/busPrdTraceInfo_update/{busPrdTraceInfoId}")
    public String busPrdTraceInfoUpdate(@PathVariable Integer busPrdTraceInfoId, Model model) {
        BusPrdTraceInfo busPrdTraceInfo = busPrdTraceInfoService.selectById(busPrdTraceInfoId);
        model.addAttribute("item",busPrdTraceInfo);
        LogObjectHolder.me().set(busPrdTraceInfo);
        return PREFIX + "busPrdTraceInfo_edit.html";
    }

    /**
     * 获取BusPrdTraceInfo列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Map map) {
        Integer comId=ShiroKit.getUser().getDeptId();
        Dept dept=deptService.selectById(comId);
        Integer comPid=dept.getPid();
        KaptchaUtil.comAuth(map,comPid,String.valueOf(comId));
        return busPrdTraceInfoService.selectTraceList4Com(map);
    }
    /**
     * 获取BusPrdTraceInfo列表
     */
    @RequestMapping(value = "/listSearch")
    @ResponseBody
    public Object listSearch(@RequestParam(required = false) String comId, @RequestParam(required = false) String prdId) {
        Map map=new HashMap();
        map.put("comId",comId);
        map.put("prdId",prdId);
        Integer userComId=ShiroKit.getUser().getDeptId();
        Dept dept=deptService.selectById(userComId);
        Integer comPid=dept.getPid();
        KaptchaUtil.comAuth(map,comPid,String.valueOf(userComId));

        return busPrdTraceInfoService.selectTraceList4Com(map);
    }
    /**
     * 新增BusPrdTraceInfo
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(BusPrdTraceInfo busPrdTraceInfo) {
        busPrdTraceInfoService.insert(busPrdTraceInfo);
        return SUCCESS_TIP;
    }

    /**
     * 删除BusPrdTraceInfo
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String busPrdTraceInfoId) {
        busPrdTraceInfoService.deleteById(busPrdTraceInfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改BusPrdTraceInfo
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BusPrdTraceInfo busPrdTraceInfo) {
        busPrdTraceInfoService.updateById(busPrdTraceInfo);
        return SUCCESS_TIP;
    }

    /**
     * BusPrdTraceInfo详情
     */
    @RequestMapping(value = "/detail/{busPrdTraceInfoId}")
    @ResponseBody
    public Object detail(@PathVariable("busPrdTraceInfoId") Integer busPrdTraceInfoId) {
        return busPrdTraceInfoService.selectById(busPrdTraceInfoId);
    }
    /**
     * BusPrdTraceInfo详情
     */
    @RequestMapping(value = "/selectTraceList4Com")
    @ResponseBody
    public Object selectTraceList4Com(Map map) {
        Integer comId=ShiroKit.getUser().getDeptId();
        Dept dept=iDeptService.selectById(comId);
        Integer comPid=dept.getPid();
        if(comPid==0)
        {
            map.put("comId","");
        }
        return busPrdTraceInfoService.selectTraceList4Com(map);
    }

    @RequestMapping(value = "/getPrdInfo4PrdId")
    @ResponseBody
    public Object getPrdInfo4PrdId(@PathVariable("prdId") String prdId) {

        return busPrdTraceInfoService.getPrdInfo4PrdId(prdId);
    }

    @RequestMapping(value = "/traceSearch")
    @ResponseBody
    public ResponseData traceSearch(@RequestParam("busPrdTraceId") String busPrdTraceId,@RequestParam("traceAddress") String traceAddress) {
        Map map=new HashMap();
        Map returnMap=new HashMap();
        if(busPrdTraceId.length()!=Config.traceNoLength)
        {
            return new ResponseData(null, ServiceStatus.NOT_FOUND);
        }

        String encryptPrdTraceNo=busPrdTraceId;
        //String areaId=busPrdTraceId.substring(0, Config.sellAreaLength);
        String prdNo=busPrdTraceId.substring(0,5);
        String datadate= busPrdTraceId.substring(5,13);
        busPrdTraceId=busPrdTraceId.substring(13);
        busPrdTraceId= KaptchaUtil.espectTraceNo(busPrdTraceId);
        map.put("datadate",datadate);
        map.put("traceSeq",busPrdTraceId);
        map.put("encryptPrdTraceNo",encryptPrdTraceNo);
        //map.put("prdSellArea",areaId);
        map.put("prdNo",prdNo);
        //根据区间 日期 区域编号查询@TableId(value="ID", type= IdType.ID_WORKER)，如果查询则插入到历史数据中，并更新查询信息
        List<BusPrdTrace> busPrdTracesList=busPrdTraceService.selectTraceBatchByMap(map);
        //无数据
        if(busPrdTracesList.size()==0)
        {
            return new ResponseData(null, ServiceStatus.NOT_FOUND);
        }else
        {
            //查询到，进行查询历史处理
            BusPrdTrace busPrdTrace=busPrdTracesList.get(0);

            List<BusPrdTraceInfo> busPrdTraceInfoList=busPrdTraceInfoService.selectTraceInfo(map);
            BusPrdTraceInfo busPrdTraceInfo=new BusPrdTraceInfo();
            BusPrdTraceHistory busPrdTraceHistory=new BusPrdTraceHistory();
            if(busPrdTraceInfoList.size()==0)
            {
                //第一次查询的处理，作入数据
                busPrdTraceInfo.setComId(busPrdTrace.getComId());
                busPrdTraceInfo.setPrdId(busPrdTrace.getPrdId());
                busPrdTraceInfo.setPrdTraceNo(busPrdTraceId);
                busPrdTraceInfo.setTraceAddress(traceAddress);
                busPrdTraceInfo.setTraceCount(1);
                busPrdTraceInfo.setTraceTime(new Date());
                busPrdTraceInfo.setEncryptPrdTraceNo(encryptPrdTraceNo);
                //busPrdTraceInfo.setPrdSellArea(areaId);
                busPrdTraceInfo.setDatadate(Integer.parseInt(datadate));
                busPrdTraceInfoService.insert(busPrdTraceInfo);

                busPrdTraceHistory.setComId(busPrdTrace.getComId());
                busPrdTraceHistory.setCreatetime(new Date());
                busPrdTraceHistory.setPrdId(busPrdTrace.getPrdId());
                busPrdTraceHistory.setPrdTraceId(busPrdTrace.getId());
                busPrdTraceHistory.setPrdTraceNo(busPrdTraceId);
                busPrdTraceHistory.setTraceCurCount(1);
                busPrdTraceHistory.setTraceTime(new Date());
                busPrdTraceHistory.setTraceAddress(traceAddress);
                busPrdTraceHistory.setEncryptPrdTraceNo(encryptPrdTraceNo);
                //busPrdTraceHistory.setPrdSellArea(areaId);
                busPrdTraceHistory.setDatadate(Integer.parseInt(datadate));
                busPrdTraceHistoryService.insert(busPrdTraceHistory);

            }else
            {
                //多次查询的处理。作入历史数据
                busPrdTraceInfo=busPrdTraceInfoList.get(0);
                busPrdTraceHistory.setComId(busPrdTraceInfo.getComId());
                busPrdTraceHistory.setCreatetime(new Date());
                busPrdTraceHistory.setPrdId(busPrdTraceInfo.getPrdId());
                busPrdTraceHistory.setPrdTraceId(busPrdTracesList.get(0).getId());
                busPrdTraceHistory.setPrdTraceNo(busPrdTraceId);
                busPrdTraceHistory.setTraceCurCount(busPrdTraceInfo.getTraceCount()+1);
                busPrdTraceHistory.setTraceTime(new Date());
                busPrdTraceHistory.setTraceAddress(traceAddress);
                busPrdTraceHistory.setEncryptPrdTraceNo(encryptPrdTraceNo);
                //busPrdTraceHistory.setPrdSellArea(areaId);
                busPrdTraceHistory.setDatadate(Integer.parseInt(datadate));
                busPrdTraceHistoryService.insert(busPrdTraceHistory);
                //更新总次数
                busPrdTraceInfo.setTraceCount(busPrdTraceInfo.getTraceCount()+1);
                busPrdTraceInfoService.updateById(busPrdTraceInfo);

            }
            //存入列表信息，以及产品公司信息
            returnMap.put("busComPrdInfo",busPrdTraceInfoService.getPrdInfo4PrdNo(prdNo));
            returnMap.put("busPrdTraceInfoList",busPrdTraceInfoService.selectTraceList4Com(map));
        }
        return new ResponseData(returnMap,ServiceStatus.OK);
    }
    /**
     * 跳转到添加product
     */
    @RequestMapping("/toTracePage")
    public String toTracePage(@RequestParam("busPrdTraceId") String busPrdTraceId,Model model) {
        model.addAttribute("busPrdTraceId",busPrdTraceId);
        return "/bus/front/" + "traceinfo.html";
    }
}
